package com.czg.app;

import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.*;

/**
 * @author czg
 */
public class MessageDialog {
    public static void openWarning(Shell shell, String title, String message) {
        MessageBox messageBox =
                new MessageBox(shell,
                        SWT.OK |
                                SWT.ICON_WARNING);
        messageBox.setMessage(message);
        messageBox.setText(title);
        messageBox.open();
    }

    public static void openError(Shell shell, String title, String message) {
        MessageBox messageBox =
                new MessageBox(shell,
                        SWT.OK |
                                SWT.ICON_ERROR);
        messageBox.setMessage(message);
        messageBox.setText(title);
        messageBox.open();
    }

    public static void openInformation(Shell shell, String title, String message) {
        MessageBox messageBox =
                new MessageBox(shell,
                        SWT.OK |
                                SWT.ICON_INFORMATION);
        messageBox.setMessage(message);
        messageBox.setText(title);
        messageBox.open();
    }

    public static boolean openConfirm(Shell shell, String title, String message) {
        MessageBox messageBox =
                new MessageBox(shell,
                        SWT.OK |
                                SWT.CANCEL |
                                SWT.ICON_QUESTION);
        messageBox.setMessage(message);
        messageBox.setText(title);
        return messageBox.open() == SWT.YES;
    }

    public static boolean openYesNo(Shell shell, String title, String message) {
        MessageBox messageBox =
                new MessageBox(shell,
                        SWT.YES |
                                SWT.NO |
                                SWT.ICON_QUESTION);
        messageBox.setMessage(message);
        messageBox.setText(title);
        return messageBox.open() == SWT.YES;
    }

    /**
     * 是、否、取消对话框
     *
     * @param shell
     * @param title
     * @param message
     * @return SWT.YES | SWT.NO | SWT.CANCEL
     */
    public static int openYesNoCancel(Shell shell, String title, String message) {
        MessageBox mb = new MessageBox(shell,
                SWT.ICON_INFORMATION | SWT.YES | SWT.NO | SWT.CANCEL);
        mb.setText(title);
        mb.setMessage(message);
        return mb.open();
    }

    /**
     * 中止、重试、忽略对话框
     *
     * @param shell
     * @param title
     * @param message
     * @return SWT.ABORT | SWT.RETRY | SWT.IGNORE
     */
    public static int openAbortRetryIgnore(Shell shell, String title, String message) {
        // 显示问题图标，终止、重试和忽略按钮
        MessageBox mb = new MessageBox(shell,
                SWT.ICON_QUESTION | SWT.ABORT | SWT.RETRY | SWT.IGNORE);
        mb.setText(title);
        mb.setMessage(message);
        return mb.open();
    }


    /**
     * 文件选择对话框
     *
     * @param parent
     * @param filterPath       初始路径
     * @param filterExtensions 扩展名过滤 new String[] { "*.txt", "*.doc" }
     * @param isMulti          是否多选
     */
    public static String fileDialog(Shell parent, String filterPath, String[] filterExtensions, boolean isMulti) {
        //新建文件对话框，并设置为打开的方式
        FileDialog filedlg = new FileDialog(parent, isMulti ? (SWT.OPEN | SWT.MULTI) : SWT.OPEN);
        //设置文件对话框的标题
        filedlg.setText("文件选择");
        //设置初始路径
        filedlg.setFilterPath(filterPath);
        filedlg.setFilterExtensions(filterExtensions);
        //打开文件对话框，返回选中文件的绝对路径
        return filedlg.open();
    }


    /**
     * 文件夹（目录）选择对话框
     */
    /**
     * 文件夹（目录）选择对话框
     *
     * @param parent
     * @param filterPath 初始路径
     * @return
     */
    public static String folderDialog(Shell parent, String filterPath) {
        //新建文件夹（目录）对话框
        DirectoryDialog folderdlg = new DirectoryDialog(parent);
        //设置文件对话框的标题
        folderdlg.setText("文件夹选择");
        //设置初始路径
        folderdlg.setFilterPath(filterPath);
        //设置对话框提示文本信息
        folderdlg.setMessage("请选择相应的文件夹");
        //打开文件对话框，返回选中文件夹目录
        String selecteddir = folderdlg.open();
        if (selecteddir == null) {
            return null;
        } else {
            System.out.println("您选中的文件夹目录为：" + selecteddir);
            return selecteddir;
        }
    }

    public static String openInputDialog(Shell shell, String title, String message) {
        return openInputDialog(shell, title, message, null, null);
    }

    public static String openInputDialog(Shell shell, String title, String message, String initialValue) {
        return openInputDialog(shell, title, message, initialValue, null);
    }

    public static String openInputDialog(Shell shell, String title, String message, String initialValue, IInputValidator validator) {
        InputDialog dialog = new InputDialog(shell, title, message, initialValue, validator);
        int result = dialog.open();
        if (result == InputDialog.OK) {
            return dialog.getValue();
        }
        return null;
    }

    public static RGB openColorDialog(Shell shell) {
        ColorDialog dialog = new ColorDialog(shell);
        return dialog.open();
    }

    public static FontData openFontDialog(Shell shell) {
        FontDialog dialog = new FontDialog(shell);
        return dialog.open();
    }

}
